package com.qinghe.plant.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qinghe.plant.util.CodeType;
import com.qinghe.plant.util.Response;
import com.qinghe.plant.dao.Facility;
import com.qinghe.plant.model.FacilityReponsitory;

@RestController
public class FacilityController {

	@Autowired
	private FacilityReponsitory facilityReponsitory;	
	
	@RequestMapping(value = "doReadFacilitys", method = RequestMethod.POST)
	public Response ReadFacilitys(HttpServletRequest request) {
		List<Facility> facilities= FindFacilitys(request.getParameter("mid"));
		return new Response(CodeType.Success,JSONArray.toJSONString(facilities),"操作成功");
	}
	
	@RequestMapping(value = "doReadFacility", method = RequestMethod.POST)
	public Response ReadFacility(HttpServletRequest request) {
		String mid =request.getParameter("mid");
		String aid=request.getParameter("aid");
		String eid =request.getParameter("eid");
		Facility facility = FindFacility(mid,aid,eid);
		return new Response(CodeType.Success,JSONObject.toJSONString(facility),"操作成功");
	}

	@RequestMapping(value = "doDeleteFacility", method = RequestMethod.POST)
	public Response DeleteFacility(HttpServletRequest request) {
		String id =request.getParameter("id");
		facilityReponsitory.deleteById(id);
		return new Response(CodeType.Success,null,"操作成功");
	}
	
	@RequestMapping(value="doAlterFacility",method=RequestMethod.POST)
	public Response AlterFacility(HttpServletRequest request)
	{
		List<Facility> facilities=JSONArray.parseArray(request.getParameter("facilities"), Facility.class);
		for (int i = 0; i <facilities.size(); i++) {
			Facility temp=facilityReponsitory.save(facilities.get(i));
			facilities.set(i, temp);
		}
		return new Response(CodeType.Success,JSONArray.toJSONString(facilities),"操作成功");		
	}
	
	@RequestMapping(value="doInsertFacility",method=RequestMethod.POST)
	public Response InsertFacility(HttpServletRequest request)
	{
		if(HasFacility(request.getParameter("mid"),request.getParameter("aid"),request.getParameter("eid")))
		{
			return new Response(CodeType.Failed,null,"数据已经存在！");
		}
		else
		{
			Facility facility=new Facility();
			facility.setMid(request.getParameter("mid"));
			facility.setAid(request.getParameter("aid"));
			facility.setEid(request.getParameter("eid"));
			facility.setDid(request.getParameter("did"));
			facility.setIndex(Integer.parseInt(request.getParameter("index")));
			facility.setPosition(request.getParameter("position"));
			facility.setRotation(request.getParameter("rotation"));
			facility.setScale(request.getParameter("scale"));
			facility.setCustom(request.getParameter("custom"));
			facility.setCreator(request.getParameter("creator"));			
			facility.setTimer(new Date());
			facility= facilityReponsitory.save(facility);
			return new Response(CodeType.Success,JSONObject.toJSONString(facility),"操作成功");
		}
	}
	
	private List<Facility> FindFacilitys(String mid) {
		@SuppressWarnings("serial")
		List<Facility> result = facilityReponsitory.findAll(new Specification<Facility>() {
			@Override
			public Predicate toPredicate(Root<Facility> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				list.add(cb.equal(root.get("mid").as(String.class), mid));

				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		});
		return result;
	}	
		
	private Facility FindFacility(String mid,String aid,String eid) {
		@SuppressWarnings("serial")
		List<Facility> result = facilityReponsitory.findAll(new Specification<Facility>() {
			@Override
			public Predicate toPredicate(Root<Facility> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();

				list.add(cb.equal(root.get("mid").as(String.class), mid));
				list.add(cb.equal(root.get("aid").as(String.class), aid));
				list.add(cb.equal(root.get("eid").as(String.class), eid));
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}

		});

		return (result.size()!=0)?result.get(0):null;
	}		
	
	private Boolean HasFacility(String mid,String aid,String eid) {
		@SuppressWarnings("serial")
		List<Facility> result = facilityReponsitory.findAll(new Specification<Facility>() {
			@Override
			public Predicate toPredicate(Root<Facility> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();

				list.add(cb.equal(root.get("mid").as(String.class), mid));
				list.add(cb.equal(root.get("aid").as(String.class), aid));
				list.add(cb.equal(root.get("eid").as(String.class), eid));
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}

		});

		return (result.size()!=0)?true:false;
	}		
}
